package com.jsgt.greekfire.common.http

import com.google.gson.*
import java.lang.reflect.Type

class IntegerDefault0Adapter : JsonSerializer<Int>, JsonDeserializer<Int> {
    override fun serialize(src: Int?, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement = JsonPrimitive(src)
    override fun deserialize(json: JsonElement, typeOfT: Type?, context: JsonDeserializationContext?): Int {
        try {
            if (json.asString == "" || json.asString == "null") {//定义为int类型,如果后台返回""或者null,则返回0
                return 0
            }
        } catch (ignore: Exception) {
        }
        try {
            return json.asInt
        } catch (e: Exception) {
            //throw JsonSyntaxException(e)
            return try {
                val str = json.asString
                str.toDouble().toInt()
            } catch (ex: Exception) {
                0
            }
        }
    }
}

class DoubleDefault0Adapter : JsonSerializer<Double>, JsonDeserializer<Double> {
    override fun serialize(src: Double?, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement = JsonPrimitive(src)
    override fun deserialize(json: JsonElement, typeOfT: Type?, context: JsonDeserializationContext?): Double {
        try {
            if (json.asString == "" || json.asString == "null") {//定义为double类型,如果后台返回""或者null,则返回0.00
                return 0.00
            }
        } catch (ignore: Exception) {
        }
        try {
            return json.asDouble
        } catch (e: NumberFormatException) {
            //throw JsonSyntaxException(e)
            return 0.0
        }
    }
}

class LongDefault0Adapter : JsonSerializer<Long>, JsonDeserializer<Long> {
    override fun serialize(src: Long?, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement = JsonPrimitive(src)
    override fun deserialize(json: JsonElement, typeOfT: Type?, context: JsonDeserializationContext?): Long {
        try {
            if (json.asString == "" || json.asString == "null") {//定义为long类型,如果后台返回""或者null,则返回0
                return 0L
            }
        } catch (ignore: Exception) {
        }
        try {
            return json.asLong
        } catch (e: NumberFormatException) {
            //throw JsonSyntaxException(e)
            return 0L
        }
    }
}

class BooleanDefaultAdapter : JsonSerializer<Boolean>, JsonDeserializer<Boolean> {
    override fun serialize(src: Boolean?, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement = JsonPrimitive(src)
    override fun deserialize(json: JsonElement, typeOfT: Type?, context: JsonDeserializationContext?): Boolean {
        try {
            if (json.asString == "" || json.asString == "null") {
                return false
            }
        } catch (ignore: Exception) {
        }
        try {
            val jsonString = json.asString
            return "y" == jsonString.toLowerCase() || "yes" == jsonString.toLowerCase() ||
                    "true" == jsonString.toLowerCase() || "1" == jsonString || json.asBoolean
        } catch (e: NumberFormatException) {
            //throw JsonSyntaxException(e)
            return false
        }
    }
}

class FloatDefault0Adapter : JsonSerializer<Float>, JsonDeserializer<Float> {
    override fun serialize(src: Float?, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement = JsonPrimitive(src)
    override fun deserialize(json: JsonElement, typeOfT: Type?, context: JsonDeserializationContext?): Float {
        return try {
            json.asFloat
        } catch (e: Exception) {
            0.0f
        }
    }
}